package net.blogjava.mobile.calendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

// 所有记录与提醒
public class AllRecord extends ListActivity {
	public static int year, month, day;
	
	public static List<String> recordArray;
	public static ArrayAdapter<String> arrayAdapter;
	public static List<Integer> idList = new ArrayList<Integer>();
	public static ListActivity myListActivity;
	private MenuItem miNewRecord;
	private MenuItem miModifyRecord;
	private MenuItem miDeleteRecord;
	private OnEditRecordMenuItemClick editRecordMenuItemClick = new OnEditRecordMenuItemClick(this);

	
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		// 返回当前年、月、日
		year = getIntent().getExtras().getInt("year");
		month = getIntent().getExtras().getInt("month");
		day = getIntent().getExtras().getInt("day");

		// 参数提供Date条件用来查询记录
		Cursor cursor = Grid.dbService.query(year + "-" + month + "-" + day);
		if (recordArray == null)
			recordArray = new ArrayList<String>();
		if (arrayAdapter == null)
			arrayAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, recordArray);		// 参数：（当前Context，资源ID，代表ListView对象）
		else
			arrayAdapter.clear();	// 移除集合中所有元素

		idList.clear();		// 移除集合中所有元素,将其置空
		
		while (cursor.moveToNext()) {	// 游标向下移一位
			arrayAdapter.add(cursor.getString(1));		// 返回记录的标题
			idList.add(cursor.getInt(0));	// 返回记录的ID
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");		// 格式日期
		java.util.Calendar calendar = java.util.Calendar.getInstance();		// getInstance：构造一个新的Calendar子类实例关联当地时区
		calendar.set(year, month, day);
		setTitle(sdf.format(calendar.getTime()));	// Activity 头部标题
		setListAdapter(arrayAdapter);	// 为ListView提供游标Cursor
		myListActivity = null;
		myListActivity = this;

	}

	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		//参数：ListView对象，被点击的View，被点击View的位置，被点击View的ID
		super.onListItemClick(l, v, position, id);
		startEditRecordActivity(position);		// 跳转到另一Activity
	}

	class MenuItemClickParent {
		protected Activity activity;

		public MenuItemClickParent(Activity activity) {
			this.activity = activity;
		}
	}

	// 添加记录
	class OnAddRecordMenuItemClick extends MenuItemClickParent implements OnMenuItemClickListener {

		
		public boolean onMenuItemClick(MenuItem item) {
			Intent intent = new Intent(activity, Record.class);		// 跳转到新建Record界面
			activity.startActivity(intent);
			return true;
		}

		public OnAddRecordMenuItemClick(Activity activity) {
			super(activity);
		}

	}

	public void startEditRecordActivity(int index) {
		Intent intent = new Intent(this, Record.class);
		intent.putExtra("edit", true);
		intent.putExtra("id", idList.get(index));
		intent.putExtra("index", index);
		startActivity(intent);
	}

	// 修改记录
	class OnEditRecordMenuItemClick extends MenuItemClickParent implements OnMenuItemClickListener {

		
		public boolean onMenuItemClick(MenuItem item) {
			AllRecord allRecord = (AllRecord) activity;

			int index = allRecord.getSelectedItemPosition();	// 得到当前被选中的Item位置

			if (index < 0)
				return false;
			allRecord.startEditRecordActivity(index);	// 打开被选中的Item修改界面
			return true;
		}

		public OnEditRecordMenuItemClick(Activity activity) {
			super(activity);
		}

	}

	// 删除记录
	class OnDeleteRecordMenuItemClick extends MenuItemClickParent implements OnMenuItemClickListener {

		public OnDeleteRecordMenuItemClick(Activity activity) {
			super(activity);
		}

		
		public boolean onMenuItemClick(MenuItem item) {
			AllRecord allRecord = (AllRecord) activity;
			int index = allRecord.getSelectedItemPosition();	// 得到当前被选中的Item位置

			if (index < 0)	// 没有记录
				return false;
			
			recordArray.remove(index);		// 从List中移除指定位置的对象
			int id = idList.get(index);		// 返回List中指定位置的元素
			idList.remove(index);
			allRecord.setListAdapter(arrayAdapter);
			Grid.dbService.deleteRecord(id);	// 删除对应ID的记录
			return true;
		}

	}

	
	public boolean onCreateOptionsMenu(Menu menu) {
		miNewRecord = menu.add(0, 1, 1, "添加记录");
		miModifyRecord = menu.add(0, 2, 2, "修改/查看");
		miDeleteRecord = menu.add(0, 4, 4, "删除记录");

		miNewRecord.setOnMenuItemClickListener(new OnAddRecordMenuItemClick(this));
		miModifyRecord.setOnMenuItemClickListener(editRecordMenuItemClick);
		miDeleteRecord.setOnMenuItemClickListener(new OnDeleteRecordMenuItemClick(this));
		return true;
	}

}
